<?php
$form = $this->beginWidget('CActiveForm', array(
    'htmlOptions' => array(
	'class' => 'form-horizontal'
	)));
?>

<div class="control-group ">
    <label class="control-label" for="User_username">Type:</label>
    <div class="controls">
	<?php echo $form->dropDownList($model, 'type', Question::getListType()); ?>	
    </div>
</div>
<div class="control-group ">
    <label class="control-label" for="User_username">Text:</label>
    <div class="controls">
	<?php echo $form->textArea($model, 'text'); ?>
    </div>
</div>

<?php if ($type == Question::TYPE_QUESTION) { ?>
    <div class="control-group ">
        <label class="control-label" for="User_username">Answer:</label>
        <div class="controls">
	    <?php
	    if ($model->list_answer) {
		$aAnwser = unserialize($model->list_answer);
		foreach ($aAnwser as $key => $value) {
		    ?>
	    	<div>
	    	    <input value="<?php echo $value ?>" name="answer[]" type="text"/> <i class="icon-remove remove-answer"></i>
	    	</div>
		    <?php
		}
	    }
	    ?>
    	<a href="javascript:void(0)" id="insert-answer"><i class="icon-plus"></i> Insert answer</a>
        </div>
    </div>
<?php } ?>

<div class="form-actions">
    <?php
    $this->widget('bootstrap.widgets.TbButton', array(
	'buttonType' => 'submit',
	'type' => 'primary',
	'label' => $model->isNewRecord ? 'Create' : 'Save',
    ));
    ?>
</div>
<?php $this->endWidget(); ?>

<script>
    $(document).ready(function() {
	$('#Question_type').change(function() {
	    var type = $(this).val();
<?php if ($model->isNewRecord) { ?>
    	    window.location.assign("<?php echo url('question/add') ?>?type=" + type);
<?php } else { ?>
    	    window.location.assign("<?php echo url('question/update') ?>?id=<?php echo $model->question_id ?>&type=" + type);
<?php } ?>
	});
	$('#insert-answer').click(function() {
	    $(this).before("<div><input type='text' name='answer[]'/> <i class='icon-remove remove-answer'></i></div>");
	});
	$(document).on('click', '.remove-answer', function() {
	    $(this).parent().remove();
	});
    });
</script>